﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using COFE.Core;
using COFE.Core.Filter;
using COFE.Core.Utils;

namespace COFE.Core
{
    /// <summary>
    /// Entry list that list automatically, based on the input parameters.
    /// </summary>
    public interface IAutoEntryList : IEntryList
    {
        /// <summary>
        /// Current Status of the entries list, whether it's loading.
        /// </summary>
        StatusType Status { get; }   

        /// <summary>
        /// To wait till the entries list is completed loading, one can use EntriesLock.WaitOne()
        /// </summary>
        ManualResetEvent EntriesLock { get; }

        /// <summary>
        /// This store the excepton if the auto list failed.
        /// </summary>
        Exception Error { get; }

        /// <summary>
        /// Directory to start looking.
        /// </summary>
        IDirectoryInfo[] RootDirectories { get; }

        /// <summary>
        /// Determine what to include in the entries list.
        /// </summary>
        IEntryFilter[] EntryFilters { get; }

        /// <summary>
        /// Determine whether to continue lookup a directory.
        /// </summary>
        IOptionFilter[] OptionFilters { get; }

        /// <summary>
        /// Refresh Entries.
        /// </summary>
        void Refresh();
    }

    public static class IAutoEntryListExtension
    {
        /// <summary>
        /// Get a string that represent the current entries list.
        /// </summary>
        /// <param name="aList"></param>
        /// <param name="includeRoot">Include RootDirectory parsename in the output.</param>
        /// <returns></returns>
        public static string GetFilterString(this IAutoEntryList aList)
        {
            return COFEShared.FilterParser.
                FilterToFilterString(
                aList.EntryFilters.Concat<IFilter>(aList.OptionFilters)
                .ToArray());            
        }

        public static Guid GetId(this IAutoEntryList aList)
        {
            return aList.GetFilterString().GetHashGuid();
        }
    }
}
